package cn.jdy.plugins.service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.RedPacket;
import cn.jdy.plugins.entity.RedPacket;

/**
 * @desc 红包 订单服务类
 * @author ningqian
 * @date 2017-4-11
 */
@Service
public class RedPacketService extends BaseDao<RedPacket> {

	public long updateOrder(String orderNo) {
		try {
		  	  String sql = "select userId,count(id)as zs from EvaluationRecord where liveId=? and userId=? ";
		  	      SQLQuery sqlQuery = em().createSQLQuery(sql);
		  	      sqlQuery.setParameter(0,orderNo);
		  	 // 将查询寻结果转换为Map格式，解决多表查询或零散字段查询 映射取值问题
			   
				 
			    return   1;
			    } catch (Exception e) {
			      BaseLog.e(this.getClass(), "搜索模糊查询异常！", e);
			    }
			    return 0;
	  }
	
	public List<RedPacket> query(RedPacket obj, String... date) {
		if(null == obj) return super.findAll();
		if(obj.getiDisplayLength() > 0){
		  return super.find(createFilter(obj, date),obj.getiDisplayStart(),obj.getiDisplayLength());
		}
		return super.find(createFilter(obj, date));
	}

	public Long count(RedPacket obj, String... date) {
		return super.count(createFilter(obj, date));
	}
	
	/**
	 * 拼装条件(直播id、创建时间)
	 * @param Kindergarten
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(RedPacket obj, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != obj.getLiveId()){
		  dc.add(Restrictions.eq("liveId", obj.getLiveId()));
	  }
	  
	 /* if(StringUtil.isNotEmpty(obj.getRpType())){
		  dc.add(Restrictions.eq("rpType", obj.getRpType()));

	  }*/
	  if(StringUtil.isNotEmpty(obj.getUserNickName())){
		  dc.add(Restrictions.eq("userNickName", obj.getUserNickName()));
	  }
	  
		  dc.add(Restrictions.eq("sendStatus", "1"));

	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  
	  if(StringUtil.isNotEmpty(obj.getRpType())&&obj.getRpType().equals("1")){
			dc.add(Restrictions.or(Restrictions.eq("rpType", "1"),Restrictions.eq("rpType", "2")));

	  }else{
		  dc.add(Restrictions.eq("rpType", obj.getRpType()));
	  }
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}
	/**
	获取总钱数
	 */
	public String countZje(Long liveId,String rpType) {
		// TODO Auto-generated method stub
		 String sql="";
		if(rpType.equals("1")){
			  sql=" select ROUND(sum(total),2) as zs from RedPacket where liveId="+liveId+" and sendStatus=1 and (rpType=1 or rpType=2)  ";

		}else{
			  sql=" select ROUND(sum(total),2) as zs from RedPacket where liveId="+liveId+" and sendStatus=1 and rpType="+rpType+" ";

		}
		        SQLQuery sqlQuery1 = super.em().createSQLQuery(sql);
		        sqlQuery1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			      List<Map> list1= sqlQuery1.list();
			      Map<String,Object> map2=new  HashMap<String,Object>();
			      Map<String,Double> map1=  list1.get(0);
			      String sTring=map1.get("zs")+"";
		return sTring;
	}
	
	
	
	
 
 
 
 
	
}
